#include "PreCompiled.h"
#include "NavigationStyle.h"
#include "view3DInventorViewer.h"
#include "MenuManager.h"
#include "Application.h"

TYPESYSTEM_SOURCE_ABSTRACT(NavigationStyle,Base::BaseClass);
NavigationStyle::NavigationStyle(void)
{
	ctrldown = shiftdown = button1down = button2down = button3down = false;
}

NavigationStyle::~NavigationStyle(void)
{
}

void NavigationStyle::setPopupMenuEnabled(bool bl)
{

}

bool NavigationStyle::isPopupMenuEnabled()const
{
  return true;
}

void NavigationStyle::openPopupMenu(/*QPoint pos*/)
{
   MenuItem* view = new MenuItem;
   Application::Instance()->setupContextMenu("View",view);
    
    QMenu  contextMenu(viewer);
    QMenu subMenu;
    QActionGroup subMenuGroup(&subMenu);
    subMenuGroup.setExclusive(true);
    subMenu.setTitle(QObject::tr("Navigation styles"));
    MenuManager::Instance()->setupContextMenu(view,contextMenu);
    contextMenu.addMenu(&subMenu);

	QAction* item = subMenuGroup.addAction("name");
	item->setData(QByteArray("dd"));
	item->setCheckable(true);
	subMenu.addAction(item);

	delete view;

	QAction* used = contextMenu.exec(QCursor::pos());

	if(!used)
	{
		int a = 0;
	}
// 	if (used && subMenuGroup.actions().indexOf(used) >= 0 && used->isChecked()) {
// 		QByteArray type = used->data().toByteArray();
// 		QWidget* widget = viewer;
// 		while (widget && !widget->inherits("Gui::View3DInventor"))
// 			widget = widget->parentWidget();
// 		if (widget) {
// 			// this is the widget where the viewer is embedded
// 			Base::Type style = Base::Type::fromName((const char*)type);
// 			if (style != this->getTypeId()) {
// 				QEvent* event = new NavigationStyleEvent(style);
// 				QApplication::postEvent(widget, event);
// 			}
// 		}
// 	}

}
void NavigationStyle::setViewer(view3DInventorViewer*view)
{
  this->viewer = view;
}
void NavigationStyle::setViewingMode(const ViewerMode newmode)
{
	const ViewerMode oldmode = this->currentmode;
	if (oldmode == newmode)
	{
		return;
	}
    this->currentmode = newmode;
}
int NavigationStyle::getViewingMode()const
{
   return (int)this->currentmode; 
}

bool NavigationStyle::processEvent(QEvent* e)
{
	return true;
}
TYPESYSTEM_SOURCE_ABSTRACT(UserNavigationStyle,NavigationStyle);
// ---------------------------------------------------- [4/23/2013 Administrator]
TYPESYSTEM_SOURCE(CADNavigationStyle,UserNavigationStyle);
CADNavigationStyle::CADNavigationStyle()
{
  
};
CADNavigationStyle::~CADNavigationStyle()
{

};
const char* CADNavigationStyle::mouseButtons(ViewerMode mode)
{
  return "";
}
bool CADNavigationStyle::processEvent(QEvent* e)
{
	//VxRect rt = viewer->getViewRect();
	const Vx2DVector prevnormalized = this->lastmouseposition;

    bool processed = FALSE;
	const ViewerMode curmode = this->currentmode;
	ViewerMode newmode = curmode;

	//keyboard handling

	bool keypress = e->type() == QEvent::KeyPress? TRUE:FALSE;

	QKeyEvent* ke = static_cast<QKeyEvent*>(e);
	if(ke->key() == Qt::Key_A)
	{
        key_A_Down = keypress;
	}

	bool mousepress = e->type() == QEvent::MouseButtonPress?TRUE:FALSE;

	QMouseEvent* mousebutton = static_cast<QMouseEvent*>(e);

		if(mousebutton->button() == Qt::LeftButton)
		{
			this->button1down = mousepress;

		}else if(mousebutton->button() == Qt::MidButton)
		{
			this->button3down = mousepress;
		}else if(mousebutton->button() == Qt::RightButton)
		{
			this->button2down = mousepress;
			if(!mousepress)
			{
				if(isPopupMenuEnabled())
				{
					this->openPopupMenu(/*QCursor::pos()*/);
				}
			}
		}
		if(mousepress || keypress)
		{
		if(QApplication::keyboardModifiers() == (Qt::AltModifier | Qt::ControlModifier))
		{
			this->ctrl_alt_down = true;

		}else
		{
			if(QApplication::keyboardModifiers() == Qt::ControlModifier)
			{
				this->ctrldown = true;
			}
			if(QApplication::keyboardModifiers() == Qt::ShiftModifier)
			{
				this->shiftdown = true;
			}
			if(QApplication::keyboardModifiers() == Qt::AltModifier)
			{
				this->altdown = true;
			}
		}
	}

		
	enum {
		BUTTON1DOWN        = 1 << 0,
		BUTTON3DOWN        = 1 << 1,
		CTRLDOWN           = 1 << 2,
		SHIFTDOWN          = 1 << 3,
		BUTTON2DOWN        = 1 << 4,
		ALTDOWN            = 1 << 5,
		CONTROL_ALT_DOWN   = 1 << 6,

		KEY_A_DOWN         = 1 << 7
	};
	unsigned int combo =
		(this->button1down ? BUTTON1DOWN : 0) |
		(this->button2down ? BUTTON2DOWN : 0) |
		(this->button3down ? BUTTON3DOWN : 0) |
		(this->ctrldown ? CTRLDOWN : 0) |
		(this->shiftdown ? SHIFTDOWN : 0)|
		(this->altdown ? ALTDOWN :0)|
		(this->ctrl_alt_down ? CONTROL_ALT_DOWN : 0)|
		(this->key_A_Down ? KEY_A_DOWN : 0);



	switch(combo)
	{
	 case 0:
		 if(curmode == NavigationStyle::SPINNING){break;}
		 newmode = NavigationStyle::IDLE;
		 break;
	 case  BUTTON1DOWN:
		 {
			 if(curmode == NavigationStyle::SPINNING)
				 newmode = NavigationStyle::IDLE;
			 else
				 newmode = NavigationStyle::SELECTION;

			 button1down = false;

			 viewer->processSelection(combo);
		 }
		 break;
	 case BUTTON2DOWN:
		 {
			 bool b = ctrldown;
			 button2down = false;
		 }
		 break;
	 case  CTRLDOWN|BUTTON2DOWN:
		 {
			 bool b = altdown;
			 ctrldown = button2down = false;
		 }
		 break;
	 case  SHIFTDOWN|BUTTON2DOWN:
        shiftdown = button2down = false;
		 break;
	 case CONTROL_ALT_DOWN|BUTTON2DOWN:
		this->ctrl_alt_down = button1down = false;
		 break;
	 case CONTROL_ALT_DOWN |KEY_A_DOWN:
		 this->ctrl_alt_down = key_A_Down = false;
		break;
	 case CTRLDOWN|KEY_A_DOWN:
         int b = 0;
		 break;

	}
     ctrldown = shiftdown = altdown = ctrl_alt_down = key_A_Down = false;
	if(newmode != curmode)
		this->setViewingMode(newmode);





   
    return true;
}
